# Copyright 2018- The Pixie Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

load("//bazel:pl_build_system.bzl", "pl_cc_binary", "pl_cc_library", "pl_cc_test")

package(default_visibility = ["//src/stirling:__subpackages__"])

pl_cc_library(
    name = "cc_library",
    srcs = glob(
        ["*.cc"],
        exclude = [
            "**/*_test.cc",
            "probe_cleaner_standalone.cc",
        ],
    ),
    hdrs = glob(
        [
            "*.h",
        ],
    ),
    # This is a critical define. The cmake build uses this define,
    # and if we don't include it, the headers will mismatch the object files,
    # resulting in obscure segmentation faults around BpfOrc.
    # For reference, see the cmake build in bpftrace.BUILD.
    # TODO(oazizi): See if we can contribute to the bpftrace repo to help with this case.
    defines = ["LLVM_ORC_V2"],
    deps = [
        "//src/common/system:cc_library",
        "//src/stirling/obj_tools:cc_library",
        "//src/stirling/utils:cc_library",
        "//src/stirling/bpf_tools/bcc_bpf_intf:cc_library",
        "//src/stirling/bpf_tools/bcc_bpf:task_struct_mem_read",
    ] + select({
        "@bazel_tools//src/conditions:linux_x86_64": [
            "@com_github_iovisor_bcc//:bcc",
            "@com_github_iovisor_bpftrace//:bpftrace",
            "//:llvm",
        ],
        "//conditions:default": [],
    }),
)

pl_cc_binary(
    name = "probe_cleaner_standalone",
    srcs = ["probe_cleaner_standalone.cc"],
    deps = [":cc_library"],
)

pl_cc_test(
    name = "macros_test",
    srcs = ["macros_test.cc"],
    deps = [
        ":cc_library",
        "//src/stirling/bpf_tools/testdata:test_txt_cc_resource",
    ],
)

pl_cc_test(
    name = "bcc_wrapper_bpf_test",
    srcs = ["bcc_wrapper_bpf_test.cc"],
    tags = ["requires_bpf"],
    deps = [
        ":cc_library",
        "//src/stirling/bpf_tools/testdata:get_tgid_start_time",
        "//src/stirling/obj_tools/testdata/cc:test_exe_fixture",
    ],
)

pl_cc_test(
    name = "bcc_symbolizer_bpf_test",
    srcs = ["bcc_symbolizer_bpf_test.cc"],
    tags = ["requires_bpf"],
    deps = [
        ":cc_library",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_test(
    name = "bpftrace_wrapper_bpf_test",
    srcs = ["bpftrace_wrapper_bpf_test.cc"],
    tags = ["requires_bpf"],
    deps = [
        ":cc_library",
        "//src/stirling/obj_tools/testdata/cc:test_exe_fixture",
    ],
)

pl_cc_test(
    name = "task_struct_resolver_bpf_test",
    srcs = ["task_struct_resolver_bpf_test.cc"],
    tags = [
        # TODO(oazizi): Enable this test.
        #               It fails on Jenkins because start_time and start_boottime are the same.
        "manual",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/stirling/testing:cc_library",
    ],
)

pl_cc_test(
    name = "uprobe_extra_trigger_bpf_test",
    srcs = ["uprobe_extra_trigger_bpf_test.cc"],
    tags = [
        # This test must be exclusive, because other tests that deploy the same container images
        # could cause extra BPF triggers. In fact, this is what the test internally demonstrates
        # is a problem.
        "exclusive",
        "requires_bpf",
    ],
    deps = [
        ":cc_library",
        "//src/stirling/source_connectors/socket_tracer/testing:cc_library",
    ],
)
